package com.wzj.yygh.hosp.controller.admin;

import com.wzj.yygh.common.exception.YyghException;
import com.wzj.yygh.common.result.R;
import com.wzj.yygh.common.result.ResultCode;
import com.wzj.yygh.hosp.service.IHospitalSubjectService;
import com.wzj.yygh.model.hosp.HospitalSubject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "科室管理")
@RequestMapping("/admin/hosp/hospitalSubject")
@Transactional
public class HospitalSubjectController {

    @Autowired
    private IHospitalSubjectService iHospitalSubjectService;

    @GetMapping("findAll")
    @ApiOperation(value = "查询所有科室")
    public R getAll(){
        List<HospitalSubject> hospitalSubjectList =iHospitalSubjectService.list();
        return R.ok().data("list",hospitalSubjectList);
    }

//    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
//    @ApiOperation(value = "查询科室")
//    public Result<IPage<HospitalSubject>> getByPage(@ModelAttribute HospitalSubject subject,@ModelAttribute PageVo page){
//        QueryWrapper<HospitalSubject> qw = new QueryWrapper<>();
//        if(!StringUtils.isEmpty(subject.getSubName())) {
//            qw.like("sub_name",subject.getSubName());
//        }
//        if(!StringUtils.isEmpty(subject.getSubCode())) {
//            qw.like("sub_code",subject.getSubCode());
//        }
//        return new ResultUtil<IPage<HospitalSubject>>().setData(iHospitalSubjectService.page(PageUtil.initMpPage(page),qw));
//    }

    @ApiOperation("添加科室")
    @PostMapping("saveHospitalSubject")
    public R saveDoctorSet(@RequestBody HospitalSubject hospitalSubject) {
        //调用service
        boolean save = iHospitalSubjectService.save(hospitalSubject);
        if(save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("根据ID获取科室信息")
    @GetMapping("getHospitalSubject/{id}")
    public R getDoctorSet(@PathVariable Long id) {

        HospitalSubject hospitalSubject = iHospitalSubjectService.getById(id);
        return R.ok().data("item",hospitalSubject);
    }

    @ApiOperation("修改科室信息")
    @PutMapping("updateHospitalSubject")
    public R updateDoctorSet(@RequestBody HospitalSubject hospitalSubject) {
        boolean flag = iHospitalSubjectService.updateById(hospitalSubject);
        if(flag) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }


    @ApiOperation(value = "逻辑删除医生信息")
    @DeleteMapping("{id}")
    public R removeDoctor(
            @ApiParam(value = "医生id",required = true)
            @PathVariable Long id) {
        try {
            boolean flag = iHospitalSubjectService.removeById(id);
            if(flag) {
                return R.ok().message("删除成功");
            } else {
                return R.error().message("删除失败").code(ResultCode.ERROR_DELETE);
            }
        } catch (Exception e) {
            throw new YyghException(ResultCode.ERROR,"删除科室信息错误",e);
        }
    }

    @ApiOperation("批量删除医生")
    @DeleteMapping("batchRemove")
    public R batchRemoveDoctor(@RequestBody List<Long> idList) {
        boolean result = iHospitalSubjectService.removeByIds(idList);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

}
